package com.bree.erp.jsf.view;

import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;

import org.primefaces.event.SelectEvent;
import org.primefaces.model.LazyDataModel;

import com.bree.erp.entity.User;
import com.bree.erp.jsf.datatable.LazyBreeDataModel;
import com.bree.erp.service.UserService;

@ManagedBean(name = "dtUserLazyView")
@ViewScoped
public class UserLazyView {

	private LazyDataModel<User> lazyModel;

	@EJB
	private UserService userService;

	private User selectedItem;

	private String email;
	private String firstname;
	private String lastname;

	private String currentPassword;
	private String newPassword;
	private String retypePassword;

	@PostConstruct
	public void init() {
		lazyModel = new LazyBreeDataModel<User, UserService, String>(
				userService, "");
	}

	public void onRowSelect(SelectEvent event) {
		FacesMessage msg = new FacesMessage("Item Selected",
				((User) event.getObject()).getId() + "");
		FacesContext.getCurrentInstance().addMessage(null, msg);
	}

	public void createUser() {
		userService.create(email, newPassword, firstname, lastname);
		
		email = firstname = lastname = null;
	}

	public void updateUser() {
		userService.update(email, firstname, lastname);
	}

	public void updatePassword() {
		userService.updatePassword(email, newPassword);
	}

	/**
	 * @return the lazyModel
	 */
	public LazyDataModel<User> getLazyModel() {
		return lazyModel;
	}

	/**
	 * @return the selectedItem
	 */
	public User getSelectedItem() {
		return selectedItem;
	}

	/**
	 * @param selectedItem
	 *            the selectedItem to set
	 */
	public void setSelectedItem(User selectedItem) {
		this.selectedItem = selectedItem;

		if (selectedItem != null) {
			email = selectedItem.getEmail();
			firstname = selectedItem.getFirstName();
			lastname = selectedItem.getLastName();
		} else {
			email = firstname = lastname = null;
		}
	}

	/**
	 * @return the email
	 */
	public String getEmail() {
		return email;
	}

	/**
	 * @param email
	 *            the email to set
	 */
	public void setEmail(String email) {
		this.email = email;
	}

	/**
	 * @return the firstname
	 */
	public String getFirstname() {
		return firstname;
	}

	/**
	 * @param firstname
	 *            the firstname to set
	 */
	public void setFirstname(String firstname) {
		this.firstname = firstname;
	}

	/**
	 * @return the lastname
	 */
	public String getLastname() {
		return lastname;
	}

	/**
	 * @param lastname
	 *            the lastname to set
	 */
	public void setLastname(String lastname) {
		this.lastname = lastname;
	}

	/**
	 * @return the currentPassword
	 */
	public String getCurrentPassword() {
		return currentPassword;
	}

	/**
	 * @param currentPassword
	 *            the currentPassword to set
	 */
	public void setCurrentPassword(String currentPassword) {
		this.currentPassword = currentPassword;
	}

	/**
	 * @return the newPassword
	 */
	public String getNewPassword() {
		return newPassword;
	}

	/**
	 * @param newPassword
	 *            the newPassword to set
	 */
	public void setNewPassword(String newPassword) {
		this.newPassword = newPassword;
	}

	/**
	 * @return the retypePassword
	 */
	public String getRetypePassword() {
		return retypePassword;
	}

	/**
	 * @param retypePassword
	 *            the retypePassword to set
	 */
	public void setRetypePassword(String retypePassword) {
		this.retypePassword = retypePassword;
	}

}
